
package Database;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class edit_puzzle_6 {
    public static Object[][] getcellcontent(int puid)
    {
        Object[][] resu = new Object[6][6];
        try {
            for (int i = 0; i < 6; i++) {
                for (int j = 0; j < 6; j++) {
                    resu[i][j] = "";
                }
            }
            ResultSet get= getResuQues(puid);
            while (get.next()) {
              String type=get.getString(5);
                String ans=get.getString(4);
                String temp=get.getString(6);
                String[] op=temp.split("-");
                Integer first_index=new Integer(op[0]);
                char[] element=ans.toCharArray();
                if(type.equals("0"))
                {
                    for (char c : element) {
                        int[] index=Pro.convert.number_to_table(first_index, 6);
                        resu[index[0]][index[1]]=c;
                        first_index++;
                    }
                }else
                {
                    for (char c : element) {
                        int[] index=Pro.convert.number_to_table(first_index, 6);
                        resu[index[0]][index[1]]=c;
                        first_index=first_index+6;
                    }
                }
            }
            get.close();
        } catch (Exception ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resu;
    }
    public static ResultSet getResuQues(int puid)
    {
        ResultSet set = null;
        try {
            //query to database
            PreparedStatement pe = Connect.Connect.Con().prepareStatement("select * from Pu_Question where PuzzleID=?");
            pe.setInt(1, puid);
            set = pe.executeQuery();
            Connect.Connect.Con().close();
        } catch (Exception ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return set;
    }
    public static ArrayList get_temp_ques(int puid)
    {
        ArrayList resu = new ArrayList();
        try {
            ResultSet set = getResuQues(puid);
            while(set.next())
            {
                String type="true";
                if(set.getString(5).equals("1"))
                {
                    type="false";
                }
                String que=set.getString(3);
                String ans=set.getString(4);
                String op=set.getString(6);
                String temp=que+"&&"+ans+"&&"+op+"&&"+type;
                resu.add(temp);
            }
            set.close();
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
         return resu;
    }
    //
     public static ArrayList getlistdata(int puid)
    {
        ArrayList re = new ArrayList();
        try {
            ResultSet set = edit_puzzle.contenr(puid);
            int id=1;
            while (set.next()) {
                re.add(id+"---"+set.getString(3));
                id++;
            }
            set.close();
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return re;
    }
    public static String getdetail_forque(String ques)
    {
        String re = null;
        try {
            ResultSet set = Connect.Connect.Con().createStatement().executeQuery("select * from Pu_Question where Ask='" + ques + "'");
            String temp="";
            if(set.next())
            {
                String que=set.getString(3);
                String anw=set.getString(4);
                String qtype="Vertically";
                    if(set.getInt(5)==1)
                    {
                        qtype="Horizontal";
                    }
                    temp=temp+que+"." + "( "+qtype+" )";
                    temp=temp+"\n    Asnwer: "+anw;
                    temp=temp+"\n    Position: "+set.getString(6);
            }
            re=temp;
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return re;
    }
    public static ArrayList get_opsi_ofques(String ques)
    {
        ArrayList list=new ArrayList();
        try {
            ResultSet set=Connect.Connect.Con().createStatement().executeQuery("select * from Pu_Question where Ask='" + ques + "'");
            if(set.next())
            {
                String temp=set.getString(6);
                String[] temp2=temp.split("-");
                Integer fist=new Integer(temp2[0]);
                if(set.getString(5).equals("0"))
                {
                    int fi=((fist/6)*6)+1;
                    list.add(fi);
                    fi++;
                    list.add(fi);
                    fi++;
                    list.add(fi);
                    fi++;
                    list.add(fi);
                    fi++;
                    list.add(fi);
                    fi++;
                    list.add(fi);
                }else
                {
                    //int fi=((fist/6)*6)+1;
                    int fi=(fist%6);                    
                    list.add(fi);
                    fi=fi+6;
                    list.add(fi);
                    fi=fi+6;
                    list.add(fi);
                    fi=fi+6;
                    list.add(fi);
                    fi=fi+6;
                    list.add(fi);
                    fi=fi+6;
                    list.add(fi);
                }
            }
            set.close();
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }

        return list;
    }
    public static String get_type(String ques)
    {
        String re = null;
        try {
            ResultSet set = Connect.Connect.Con().createStatement().executeQuery("select * from Pu_Question where Ask='" + ques + "'");
            if(set.next())
            {
                if(set.getString(5).equals("0"))
                {
                    re="Horizontal";
                }else
                {
                    re="Vertically";
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return re;
    }
    public static ArrayList get_temp_pe(String content)
    {
        ArrayList list=new ArrayList();
        try {
            ResultSet set = Connect.Connect.Con().createStatement().executeQuery("select * from Pu_Question where Ask='" + content + "'");
            if(set.next())
            {
                String ans=set.getString(4);
                String[] pos=set.getString(6).split("-");
                int add=1;
                if(set.getString(3).equals("0"))
                {
                     add=6;
                }
                for (int i = 0; i < pos.length; i++) {
                    String cell=ans.substring(i, i+1);
                    list.add(pos[i]+"-"+cell);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
    public static int get_puid(String content)
    {
        int kq=0;
        try {
            ResultSet set = Connect.Connect.Con().createStatement().executeQuery("select * from Pu_Question where Ask='" + content + "'");
            if(set.next())
            {
                kq=set.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kq;
    }
    public static boolean update_que(String que, String ans, String op, int id)
    {
        boolean pro=false;
        try {
            PreparedStatement st=Connect.Connect.Con().prepareStatement("update Pu_Question set [Ask]=?, Ans=?, Position=? where QueID=?");
            st.setString(1, que);
            st.setString(2, ans);
            st.setString(3, op);
            st.setInt(4, id);
            int thuchien=st.executeUpdate();
            if(thuchien!=0)
            {
                pro=true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pro;
    }
    public static boolean delete_ques(String ques)
    {
        boolean kq=false;
         try {
            PreparedStatement st=Connect.Connect.Con().prepareStatement("delete from Pu_Question where Ask=?");
            st.setString(1, ques);
            int thuchien=st.executeUpdate();
            if(thuchien!=0)
            {
                kq=true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle_6.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kq;
    }
}
